/* 
 * Copyright (C) 2001-2003 Jacek Sieka, j_s@telia.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "ADLSProperties.h"
#include "../client/ADLSearch.h"
#include "../client/HubManager.h"
// CDM EXTENSION BEGINS
#include "WinUtil.h"
//Sulan start svn 163
#include "../client/RegexpHandler.h"
//End
#include "../client/ClientProfileManager.h"
#include ".\adlsproperties.h"
// CDM EXTENSION ENDS
// Initialize dialog
LRESULT ADLSProperties::OnInitDialog(UINT, WPARAM, LPARAM, BOOL&)
{
	// Initialize combo boxes
	::SendMessage(GetDlgItem(IDC_SOURCE_TYPE), CB_ADDSTRING, 0, 
		(LPARAM)search->SourceTypeToString(ADLSearch::OnlyFile).c_str());
	::SendMessage(GetDlgItem(IDC_SOURCE_TYPE), CB_ADDSTRING, 0, 
		(LPARAM)search->SourceTypeToString(ADLSearch::OnlyDirectory).c_str());
	::SendMessage(GetDlgItem(IDC_SOURCE_TYPE), CB_ADDSTRING, 0, 
		(LPARAM)search->SourceTypeToString(ADLSearch::FullPath).c_str());
	// CDM EXTENSION BEGINS
	::SendMessage(GetDlgItem(IDC_SOURCE_TYPE), CB_ADDSTRING, 0, 
		(LPARAM)search->SourceTypeToString(ADLSearch::TTHFile).c_str());
	// CDM EXTENSION ENDS

	::SendMessage(GetDlgItem(IDC_SIZE_TYPE), CB_ADDSTRING, 0, 
		(LPARAM)search->SizeTypeToStringInternational(ADLSearch::SizeBytes).c_str());
	::SendMessage(GetDlgItem(IDC_SIZE_TYPE), CB_ADDSTRING, 0, 
		(LPARAM)search->SizeTypeToStringInternational(ADLSearch::SizeKiloBytes).c_str());
	::SendMessage(GetDlgItem(IDC_SIZE_TYPE), CB_ADDSTRING, 0, 
		(LPARAM)search->SizeTypeToStringInternational(ADLSearch::SizeMegaBytes).c_str());
	::SendMessage(GetDlgItem(IDC_SIZE_TYPE), CB_ADDSTRING, 0, 
		(LPARAM)search->SizeTypeToStringInternational(ADLSearch::SizeGigaBytes).c_str());

	// pothead starts svn221
	ctrlRaw.Attach(GetDlgItem(IDC_ADL_RAW));
	for (int n = 0; n < 11; ++n) {
		ctrlRaw.AddString(RAWSETTING(n).c_str());
	}
	ctrlRaw.SetCurSel(search->adlsRaw);
	::SendMessage(GetDlgItem(IDC_OVER_RIDE_POINTS), BM_SETCHECK, search->overRidePoints ? 1 : 0, 0L);
	// pothead ends

	// Load search data
	char* buf = new char[32];
	SetDlgItemText(IDC_SEARCH_STRING,	search->searchString.c_str());
	SetDlgItemText(IDC_DEST_DIR,		search->destDir.c_str());
	SetDlgItemText(IDC_MIN_FILE_SIZE,	search->minFileSize > 0 ? _i64toa(search->minFileSize, buf, 10) : "");
	SetDlgItemText(IDC_MAX_FILE_SIZE,	search->maxFileSize > 0 ? _i64toa(search->maxFileSize, buf, 10) : "");
	::SendMessage(GetDlgItem(IDC_IS_ACTIVE), BM_SETCHECK, search->isActive ? 1 : 0, 0L);
	::SendMessage(GetDlgItem(IDC_SOURCE_TYPE), CB_SETCURSEL, search->sourceType, 0L);
	::SendMessage(GetDlgItem(IDC_SIZE_TYPE), CB_SETCURSEL, search->typeFileSize, 0L);
	::SendMessage(GetDlgItem(IDC_AUTOQUEUE), BM_SETCHECK, search->isAutoQueue ? 1 : 0, 0L);
	// CDM EXTENSION BEGINS
	::SendMessage(GetDlgItem(IDC_IS_FORBIDDEN), BM_SETCHECK, search->isForbidden ? 1 : 0, 0L);
	::SendMessage(GetDlgItem(IDC_IS_REGEXP), BM_SETCHECK, search->isRegExp ? 1 : 0, 0L);
	SetDlgItemText(IDC_ADLS_KICK_STRING, search->kickString.c_str());
	SetDlgItemText(IDC_ADLS_POINTS, search->adlsPoints > 0 ? _itoa(search->adlsPoints, buf, 10) : "");
	SetDlgItemText(IDC_ADLS_COMMENT, search->adlsComment.c_str());
	::SendMessage(GetDlgItem(IDC_IS_CASE_SENSITIVE), BM_SETCHECK, search->isCaseSensitive ? 1 : 0, 0L);
	if (search->fromFavs)
		::SendMessage(GetDlgItem(IDC_OVERRIDE_FAV), BM_SETCHECK, 1, 0L);
	else
		::SendMessage(GetDlgItem(IDC_OVERRIDE_CUSTOM), BM_SETCHECK, 1, 0L);


	delete[] buf;
	setControlState();
	// CDM EXTENSION ENDS

	// Center dialog
	CenterWindow(GetParent());

	return FALSE;
}

// Exit dialog
LRESULT ADLSProperties::OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if(wID == IDOK)
	{
		// Update search
		// CDM EXTENSION BEGINS
		TCHAR* buf = new TCHAR[512];
		// CDM EXTENSION ENDS

		GetDlgItemText(IDC_SEARCH_STRING, buf, 256);
		search->searchString = buf;		
		GetDlgItemText(IDC_DEST_DIR, buf, 256);
		search->destDir = buf;		

		GetDlgItemText(IDC_MIN_FILE_SIZE, buf, 256);
		search->minFileSize = (_tcslen(buf) == 0 ? -1 : Util::toInt64((string)buf));
		GetDlgItemText(IDC_MAX_FILE_SIZE, buf, 256);
		search->maxFileSize = (_tcslen(buf) == 0 ? -1 : Util::toInt64((string)buf));

		search->sourceType = (ADLSearch::SourceType)::SendMessage(GetDlgItem(IDC_SOURCE_TYPE), CB_GETCURSEL, 0, 0L);
		search->typeFileSize = (ADLSearch::SizeType)::SendMessage(GetDlgItem(IDC_SIZE_TYPE), CB_GETCURSEL, 0, 0L);
		search->isActive = (::SendMessage(GetDlgItem(IDC_IS_ACTIVE), BM_GETCHECK, 0, 0L) != 0);
		search->isAutoQueue = (::SendMessage(GetDlgItem(IDC_AUTOQUEUE), BM_GETCHECK, 0, 0L) != 0);
		// CDM EXTENSION BEGINS
		search->isForbidden = (::SendMessage(GetDlgItem(IDC_IS_FORBIDDEN), BM_GETCHECK, 0, 0L) != 0);
		search->isRegExp = (::SendMessage(GetDlgItem(IDC_IS_REGEXP), BM_GETCHECK, 0, 0L) != 0);
		search->isCaseSensitive = (::SendMessage(GetDlgItem(IDC_IS_CASE_SENSITIVE), BM_GETCHECK, 0, 0L) != 0);
		if(search->isForbidden)
			switch(search->sourceType) {
				case 1 : search->destDir = STRING(FORBIDDEN_DIRS); break;
				case 2 : search->destDir = STRING(FORBIDDEN_FULLPATHS); break;
				case 3 : search->destDir = STRING(FORBIDDEN_TTHS); break;
				default : search->destDir = STRING(FORBIDDEN_FILES);
			}
		GetDlgItemText(IDC_ADLS_POINTS, buf, 32);
		search->adlsPoints = (_tcslen(buf) == 0 ? 0 : Util::toInt((string)buf));
		GetDlgItemText(IDC_ADLS_COMMENT, buf, 512);
		search->adlsComment = buf;
		// pothead starts svn221
		search->overRidePoints = (::SendMessage(GetDlgItem(IDC_OVER_RIDE_POINTS), BM_GETCHECK, 0, 0L) != 0);
		search->fromFavs = (::SendMessage(GetDlgItem(IDC_OVERRIDE_FAV), BM_GETCHECK, 0, 0L) != 0);
		search->adlsRaw = ::SendMessage(GetDlgItem(IDC_ADL_RAW), CB_GETCURSEL, 0, 0L);
		GetDlgItemText(IDC_ADLS_KICK_STRING, buf, 512);
		search->kickString = buf;
		delete[] buf;
		// pothead ends
		// CDM EXTENSION ENDS
	}

	EndDialog(wID);
	return 0;
}
// CDM EXTENSION BEGINS
LRESULT ADLSProperties::onControlChecked(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	switch (wID) {
		case IDC_IS_FORBIDDEN: setControlState(); break;
		case IDC_IS_REGEXP: setControlState(); break;
		case IDC_OVER_RIDE_POINTS: setControlState(); break;
		case IDC_OVERRIDE_FAV: setControlState(); break;
		case IDC_OVERRIDE_CUSTOM: setControlState(); break;
		default:
			break;
	}
	return 0;
}
void ADLSProperties::setControlState() {
	bool b = (IsDlgButtonChecked(IDC_IS_FORBIDDEN) != 0);
	::EnableWindow(GetDlgItem(IDC_ADLS_POINTS), b);
	// pothead starts svn221
	::EnableWindow(GetDlgItem(IDC_OVER_RIDE_POINTS), b);
	if (IsDlgButtonChecked(IDC_IS_FORBIDDEN) && IsDlgButtonChecked(IDC_OVER_RIDE_POINTS)) {
		::EnableWindow(GetDlgItem(IDC_OVERRIDE_FAV), true);
		::EnableWindow(GetDlgItem(IDC_OVERRIDE_CUSTOM), true);
		if (IsDlgButtonChecked(IDC_OVERRIDE_FAV)) {
			::EnableWindow(GetDlgItem(IDC_ADL_RAW), true);
			::EnableWindow(GetDlgItem(IDC_ADLS_KICK_STRING), false);
		} else if (IsDlgButtonChecked(IDC_OVERRIDE_CUSTOM)) {
			::EnableWindow(GetDlgItem(IDC_ADL_RAW), false);
			::EnableWindow(GetDlgItem(IDC_ADLS_KICK_STRING), true);
		}
	} else {
		::EnableWindow(GetDlgItem(IDC_OVERRIDE_FAV), false);
		::EnableWindow(GetDlgItem(IDC_OVERRIDE_CUSTOM), false);
		::EnableWindow(GetDlgItem(IDC_ADL_RAW), false);
		::EnableWindow(GetDlgItem(IDC_ADLS_KICK_STRING), false);
	}
	// pothead ends
	b = (IsDlgButtonChecked(IDC_IS_REGEXP) != 0);
	::EnableWindow(GetDlgItem(IDC_REGEXP_TESTER_TEXT), b);
	::EnableWindow(GetDlgItem(IDC_REGEXP_TESTER_BUTTON), b);
	::EnableWindow(GetDlgItem(IDC_IS_CASE_SENSITIVE), b);
}

LRESULT ADLSProperties::onMatch(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	TCHAR buf[512];
	tstring exp, text;
	GetDlgItemText(IDC_SEARCH_STRING, buf, 256);
	exp = buf;
	GetDlgItemText(IDC_REGEXP_TESTER_TEXT, buf, 256);
	text = buf;
//Sulan start 33
	MessageBox(RegexpHandler::matchExp(exp, text, (::SendMessage(GetDlgItem(IDC_IS_CASE_SENSITIVE), BM_GETCHECK, 0, 0L) != 0)).c_str(), CSTRING(REGEX_TESTER), MB_OK);
	return 0;
}

LRESULT ADLSProperties::OnBnClickedIsForbidden(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	// TODO: Add your control notification handler code here
	return 0;
}
//End 33
// CDM EXTENSION ENDS
/**
 * @file
 * $Id: ADLSProperties.cpp Last changed: 2005-07-15 14:17:57 By: Pothead for SVN: 250 Exp$
 */